package com.ruoyi.common.utils.http;



import com.ruoyi.common.utils.baidu.Util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;

public class HttpPostFile {
    public static String sendPost(String urlString, MultipartFile multipartFile) {
        String result = "";
        try {
            File file= Util.convertMultipartFileToFile(multipartFile);
            if (file.exists()) {
                HttpClient client = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost(urlString);
                MultipartEntity entity = new MultipartEntity();
                entity.addPart("auto-path", new FileBody(file));

                httpPost.setEntity(entity);
                HttpResponse response = client.execute(httpPost);

                HttpEntity responseEntity = response.getEntity();
                result = EntityUtils.toString(responseEntity);
                System.out.println(result);
            }
        } catch (Exception e) {
            //Log.error("发送POST请求出现异常！", e);
        }
        return result.toString();
    }
}
